/**
 * @author Ahammer
 * @date 2009 04
 * @update 2011 01 added 16 bit support
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package op;

import java.awt.image.renderable.ParameterBlock;
import javax.media.jai.JAI;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;


/**
 * This is the main image processing class
 * There exist two approaches:
 * A user defined JAI operator is just called 
 * or:
 * The actual processing is implemented in this class 
 */
public class IqmThresholdOperator{ 


  public IqmThresholdOperator() {
	  //WARNING: Don't declare fields here
	  //Fields declared here aren't thread safe!   
  }
//  /**
//   * This method sets the lower threshold value(s) calculated by the preset method
//   * It uses the histogram of the total image!
//   * Nicht sicher ob das thread safe ist!!!!!!!!!!!!!!!!!!!
//   */
//  private ParameterBlockJAI setPresetLowValues(ParameterBlockJAI pbJAI, int preset) {
//		int p0 = pbJAI.getIntParameter("ThresholdLow1");
//		int p1 = pbJAI.getIntParameter("ThresholdHigh1");
//		int p2 = pbJAI.getIntParameter("ThresholdLow2");
//		int p3 = pbJAI.getIntParameter("ThresholdHigh2");
//		int p4 = pbJAI.getIntParameter("ThresholdLow3");
//		int p5 = pbJAI.getIntParameter("ThresholdHigh3");
//		PlanarImage pi = (PlanarImage) pbJAI.getSource(0);
//		int numBands = pi.getNumBands();
//		ParameterBlock pb1 = new ParameterBlock();  
//		pb1.addSource(pi);  
//		pb1.add(null); //Roi
//		pb1.add(1); //Sampling
//		pb1.add(1); 
//		pb1.add(new int[]{256});    //Number of bins
//		pb1.add(new double[]{0});   //Start
//		pb1.add(new double[]{256}); //End
//		PlanarImage pi2 = JAI.create("histogram", pb1);  	
//		Histogram histo = (Histogram) pi2.getProperty("histogram");
//		double[] th = null;		
//		if (preset == 1) th = histo.getMean();
//		if (preset == 2) th = histo.getMaxEntropyThreshold();
//		if (preset == 3) th = histo.getMaxVarianceThreshold();
//		if (preset == 4) th = histo.getMinErrorThreshold();
//		if (preset == 5) th = histo.getMinFuzzinessThreshold();
//		if (preset == 6) th = histo.getModeThreshold(2);       //power of 2
//		if (preset == 7) th = histo.getPTileThreshold(0.5);    //0 bis 1
//		p0 = (int)th[0];
//		if (numBands >= 2) p2 = (int)th[1];
//		if (numBands == 3) p4 = (int)th[2];
//		if (p1 < p0) p1 = p0;
//		if (p3 < p2) p3 = p2;
//		if (p5 < p4) p5 = p4;
//		pbJAI.setParameter("ThresholdLow1",  p0);
//		pbJAI.setParameter("ThresholdHigh1", p1);
//		pbJAI.setParameter("ThresholdLow2",  p2);
//		pbJAI.setParameter("ThresholdHigh2", p3);
//		pbJAI.setParameter("ThresholdLow3",  p4);
//		pbJAI.setParameter("ThresholdHigh3", p5);
//		Board.appendTexln("IqmThresholdOperator: Thresholds: Band1:"+p0+"/"+p1+"   Band2:"+p2+"/"+p3+"  Band3:"+p4+"/"+p5);
//		return pbJAI;
//  }
  /**
   * 
   * @param ParametrBlockJAI pbJAI
   */
  public Object run(ParameterBlockJAI pbJAI){
	  
	  //this is already done by GUI;
	  //int preset = pbJAI.getIntParameter(9);
	  //if (preset > 0) pbJAI = this.setPresetLowValues(pbJAI, preset);
	  
	  Object ob = JAI.create("IqmThreshold".toLowerCase(), pbJAI, null); 
	  
	  int binarize = pbJAI.getIntParameter("Binarize");
	  int numBands = ((PlanarImage)ob).getNumBands();
	  
	  if ((binarize == 1) && (numBands > 1)){
		  //create a single banded grey image
		  double[][] m = {{ 1./3, 1./3, 1./3, 0 }};
		  ParameterBlock pb = new ParameterBlock();
		  pb.addSource(ob);
		  pb.add(m);
	      ob = JAI.create("bandcombine", pb, null);
	  }
	  return ob;
  }
}
